import {getRequest} from './api'
import {Message} from 'element-ui'

export const isNotNullORBlank = (...args)=>{
    for(var i = 0;i<args.length;i++){
        var argument = args[i];
        if(argument == null || argument == '' || argument == undefined){
            Message.warning({message:'数据不能为空!'})
            return false;
        }
    }
    return true;
}

export const initMenu = (router,store) =>{
    if(store.state.routes.length > 0){
        return;
    }
    getRequest("/config/sysmenu").then(resp=>{
        if(resp && resp.status == 200){
            var fmtRouters = formatRouters(resp.data);
            router.addRoutes(fmtRouters);
            store.commit('initMenu',fmtRouters);
            store.dispatch('connect');
        }
    })
}

export const formatRouters = (routes)=>{
    let fmRoutes =[];
    routes.forEach(router=>{
        let{
            path,
            component,
            name,
            meta,
            iconcls,
            children
        } = router;
        if(children && children instanceof Array){
            children = formatRouters(children);
        }
        let fmRouter = {
            path: path,
            component(resolve){
                console.log("component:"+component)
                if(component.startsWith("Home")){
                    // 首页
                    require(['../components/pages/' + component +'.vue'], resolve)
                }else if(component.startsWith("vBlog")){
                    // 法律法规库
                    require(['../components/table/' + component + '.vue'], resolve)
                }else if(component.startsWith("Base")){
                    // 表单提交
                    require(['../components/from/' + component + '.vue'], resolve)
                }else if(component.startsWith("Chart")){
                    // 图表展示 Chartpillar src\components\chart\Chartpillar.vue
                    console.log(" 图表展示 ")
                    require(['../components/chart/' + component + '.vue'], resolve)
                }else if(component.startsWith("Sys")){
                    // 系统管理
                    require(['../components/system/' + component + '.vue'], resolve)
                }else if(component.startsWith("Option")){
                    // 选项卡
                    require(['../components/option/' + component + '.vue'], resolve)
                }
            },
            name:name,
            iconcls : iconcls,
            meta: meta,
            children: children
        };
        fmRoutes.push(fmRouter);
    })
    return fmRoutes;
}